package com.cysion.study.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.os.IBinder
import android.support.v7.widget.RecyclerView
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.AbsListView
import android.widget.EditText
import android.widget.ScrollView

/**
 * Created by chu on 2019-07-13.
 */
class HideUtil
private constructor(activity: Activity, content: ViewGroup?) {

    init {
        var mContent = content

        if (mContent == null) {

            mContent = activity.findViewById(android.R.id.content)

        }

        getScrollView(mContent, activity)

        mContent?.setOnTouchListener { view, motionEvent ->

            dispatchTouchEvent(activity, motionEvent)

            false

        }

    }


    @SuppressLint("ClickableViewAccessibility")
    private fun getScrollView(viewGroup: ViewGroup?, activity: Activity) {

        if (null == viewGroup) {

            return

        }

        val count = viewGroup.childCount

        for (i in 0 until count) {

            val view = viewGroup.getChildAt(i)

            if (view is ScrollView) {

                view.setOnTouchListener { view1, motionEvent ->


                    dispatchTouchEvent(activity, motionEvent)


                    false

                }

            } else if (view is AbsListView) {

                view.setOnTouchListener { view12, motionEvent ->


                    dispatchTouchEvent(activity, motionEvent)


                    false

                }

            } else if (view is RecyclerView) {

                view.setOnTouchListener { view13, motionEvent ->


                    dispatchTouchEvent(activity, motionEvent)


                    false

                }

            } else if (view is ViewGroup) {

                this.getScrollView(view, activity)

            }

        }

    }

    /**
     * 触摸事件反馈
     *
     * @param mActivity
     * @param ev
     */
    private fun dispatchTouchEvent(mActivity: Activity, ev: MotionEvent) {

        if (ev.action == MotionEvent.ACTION_DOWN) {

            val v = mActivity.currentFocus

            if (null != v && isShouldHideInput(v, ev)) {

                hideSoftInput(mActivity, v.windowToken)

            }

        }

    }

    /**
     * 是否需要隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private fun isShouldHideInput(v: View, event: MotionEvent): Boolean {

        if (v is EditText) {

            val rect = Rect()

            v.getHitRect(rect)

            return !rect.contains(event.x.toInt(), event.y.toInt())

        }

        return true

    }

    /**
     * 隐藏软键盘
     *
     * @param mActivity
     * @param token
     */
    private fun hideSoftInput(mActivity: Activity, token: IBinder?) {

        if (token != null) {
            if (onHideSoftListener != null) {
                onHideSoftListener!!.onHide()
            }
            val im = mActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }


    /**
     * 触发隐藏时的监听
     */
    interface OnHideSoftListener {
        fun onHide()
    }


    companion object {

        /**
         * 初始化
         *
         * @param activity 所需要初始化的activity
         */
        fun init(activity: Activity) {
            HideUtil(activity, null)
        }

        /**
         * 带滑动列表的初始化
         */
        fun init(activity: Activity,viewGroup: ViewGroup?) {
            HideUtil(activity, viewGroup)
        }

        var onHideSoftListener: OnHideSoftListener? = null
    }

}